/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.framework;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

/**
 * 代理工厂的基类。 提供对可配置 AopProxyFactory 的便捷访问。
 * @author 二十
 */
@SuppressWarnings("serial")
public class ProxyCreatorSupport extends AdvisedSupport {

	private AopProxyFactory aopProxyFactory;

	private final List<AdvisedSupportListener> listeners = new ArrayList<>();

	/** 创建第一个 AOP 代理时设置为 true。 */
	private boolean active = false;



	public ProxyCreatorSupport() {
		this.aopProxyFactory = new DefaultAopProxyFactory();
	}


	public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
		Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
		this.aopProxyFactory = aopProxyFactory;
	}



	public void setAopProxyFactory(AopProxyFactory aopProxyFactory) {
		Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
		this.aopProxyFactory = aopProxyFactory;
	}

	/**
	 * 这个类里面已经持有了一个默认的aop的代理工厂
	 * ctrl + h 查看当前类的继承关系
	 * 当前类是 ProxyFactory的父类，所以里面的代理工厂会被默认的初始化加载
	 * @return
	 */
	public AopProxyFactory getAopProxyFactory() {
		return this.aopProxyFactory;
	}


	public void addListener(AdvisedSupportListener listener) {
		Assert.notNull(listener, "AdvisedSupportListener must not be null");
		this.listeners.add(listener);
	}


	public void removeListener(AdvisedSupportListener listener) {
		Assert.notNull(listener, "AdvisedSupportListener must not be null");
		this.listeners.remove(listener);
	}


	/**
	 * 子类应该调用它来获得一个新的 AOP 代理。 他们不应该创建一个AOP代理this作为参数。
	 */
	protected final synchronized AopProxy createAopProxy() {
		/**
		 * active属性实际上就是一个标记，在创建第一个代理对象的时候，会将他设置为true。
		 */
		if (!this.active) {
			activate();
		}
		/**
		 * 分析一下下面这行代码的流程：
		 *
		 * 1. 获取aop的代理工厂  看一下AopProxyFactory
		 * 2. 使用工厂创建一个aop的代理  ,如何创建代理的？
		 *
		 */
		return getAopProxyFactory().createAopProxy(this);
	}

	/**
	 * 修改状态，并触发所有的监听器
	 * @see AdvisedSupportListener#activated
	 */
	private void activate() {
		this.active = true;
		for (AdvisedSupportListener listener : this.listeners) {
			listener.activated(this);
		}
	}

	/**
	 * 将advice更改事件传播到所有advice的支持侦听器。
	 * @see AdvisedSupportListener#adviceChanged
	 */
	@Override
	protected void adviceChanged() {
		super.adviceChanged();
		synchronized (this) {
			if (this.active) {
				for (AdvisedSupportListener listener : this.listeners) {
					listener.adviceChanged(this);
				}
			}
		}
	}

	/**
	 * 判断属性的状态
	 */
	protected final synchronized boolean isActive() {
		return this.active;
	}

}
